//
//  Timer.m
//  Bluetooth
//
//  Created by apple on 14/11/1.
//  Copyright (c) 2014年 com.yourname. All rights reserved.
//

#import "Timer.h"
#import <Foundation/Foundation.h>
@implementation Timer
struct timer_attr all_timer[10];
-(void) timerProc:(HWND)hwnd
             uMsg:(UINT)uMsg
          idEvent:(UINT)idEvent
           dwTime:(DWORD)dwTime
{
    BOOL bFind = FALSE;
    int id;
    for (id = 0; id < 10; id++)
    {
        if (all_timer[id].timer_id == idEvent)
        {
            bFind = TRUE;
            break;
        }
    }
    if (!bFind)
    {
        return;
    }
    
    if (all_timer[id].mode == APP_TIMER_MODE_SINGLE_SHOT)
    {
        [_paintingTimer invalidate];
    }
    all_timer[id].timeout_handler(all_timer[id].p_context);
}


-(uint32_t)app_timer_create:(app_timer_id_t * )p_timer_id
                       mode:(app_timer_mode_t)mode
            timeout_handler:(app_timer_timeout_handler_t)timeout_handler
{
    int i;
    for (i = 0; i < 10; i++)
    {
        if (!all_timer[i].bUsed)
        {
            break;
        }
    }
    all_timer[i].bUsed = TRUE;
    all_timer[i].mode = mode;
    all_timer[i].timeout_handler = timeout_handler;
    all_timer[i].p_context = NULL;
    *p_timer_id = i;
    return NRF_SUCCESS;
}

-(uint32_t)app_timer_start:(app_timer_id_t)timer_id
             timeout_ticks:(uint32_t)timeout_ticks
                 p_context:(void*)p_context
{
    
    _paintingTimer = [NSTimer scheduledTimerWithTimeInterval:timeout_ticks
                                                     target:nil
                                                   selector:@selector(timerProc:uMsg:idEvent:dwTime:)
                                                   userInfo:nil
                                                    repeats:YES];
    all_timer[timer_id].p_context = p_context;
    return NRF_SUCCESS;
}

-(uint32_t) app_timer_stop:(app_timer_id_t)timer_id
{
    [_paintingTimer invalidate];
    return NRF_SUCCESS;
}

-(uint32_t)app_query_timer:(app_timer_id_t)timer_id
{
    return NRF_SUCCESS;
}
@end
